package com.qf.service.impl;

import com.qf.dao.UsersDao;
import com.qf.entity.Users;
import com.qf.service.UsersService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 2019/9/28
 * Administrator
 * springdata0928
 * 面向对象面向君  不负代码不负卿
 */
@Service
public class UsersServiceImpl implements UsersService {
    public List<Users> findByBirthdayBetween(String begin, String end) {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date begintime = simpleDateFormat.parse(begin);
            Date endtime = simpleDateFormat.parse(end);
            return usersDao.findByBirthdayBetween(begintime,endtime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public int countByUsernameContaining(String name) {
        return usersDao.countByUsernameContaining(name);
    }

    public Page findByUsernameLike(String name, Pageable pageable) {
        return usersDao.findByUsernameLike(name,pageable);
    }

    public List<Users> findByUserIdIn(List list, Sort sort) {
        return usersDao.findByUserIdIn(list, sort);
    }

    public List<Users> findByUsernameLike(String name) {
        //return usersDao.findByUsernameLike(name);
        return  usersDao.findByUsernameStartingWith(name);
    }

    @Resource
    private UsersDao usersDao;

    @Transactional
    public Users insertUsers(Users users) {
        return usersDao.save(users);
    }

    public Users login(String username, String password) {
        return usersDao.findByUsernameAndPassword(username,password);
    }
}
